package com.cyandpos.pad.utils;

import android.app.Activity;


import com.cyandpos.pad.utils.log.LogUtils;

import java.util.HashMap;

/**
 * Activity的栈管理
 *
 * Created by derek on 2017/4/14.
 */

public class AppManager {

    private HashMap<String,Activity> mActivitys;

    private static AppManager mAppManager = null;

    private AppManager(){
        mActivitys = new HashMap<>();
    }

    public static AppManager geInstance(){
        if (mAppManager == null){
            synchronized (AppManager.class){
                if (mAppManager == null){
                    mAppManager = new AppManager();
                }
            }
        }
        return mAppManager;
    }

    public  void addActivity(String tag,Activity activity){
        Activity result = mActivitys.get(tag);
        if (result == null) {
            LogUtils.e("addAlertActivity1", tag);
            mActivitys.put(tag, activity);
        } else {
            result.finish();
            result = null;
            mActivitys.put(tag, activity);
        }
    }

    public  void  removeActivity(String tag){
        if (mActivitys.get(tag) != null) {
            LogUtils.e("remove_AlertActivity1", tag);
            mActivitys.get(tag).finish();
            mActivitys.remove(tag);
        }
    }
    public  void  removeActivity(Activity activity){
        for (int i = mActivitys.size() - 1; i >= 0; i--) {
            if (mActivitys.get(i).getClass().equals(activity.getClass())) {
                mActivitys.get(i).finish();
                mActivitys.remove(i);
            }
        }
    }

    public void removeAll(){
        for (int i = mActivitys.size() - 1; i >= 0; i--) {
            mActivitys.get(i).finish();
            mActivitys.remove(i);
        }
    }


    public int getSize(){
        return mActivitys.size();
    }


}
